package com.byzoro.controller;

import com.byzoro.pojo.Models;
import com.byzoro.service.ModelsService;
import com.byzoro.utils.JsonResult;
import com.byzoro.utils.Upload;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Component
@Controller
@Slf4j
public class ModelsController {
    @Autowired
    ModelsService modelsService;

    @ResponseBody
    @GetMapping("/api/model/list")
    public JsonResult list(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        return modelsService.list(page, limit);
    }

    @GetMapping("/modelEdit/{id}")
    public String modelEdit(@PathVariable("id") int id, Model model) {
        model.addAttribute("id", id);
        return "modelEdit";
    }

    @ResponseBody
    @GetMapping("/api/model/detail")
    public JsonResult detail(@RequestParam("id") int id) {
        return modelsService.detail(id);
    }

    @ResponseBody
    @PutMapping("api/model/update")
    public JsonResult edit(@RequestBody Models models) {
        return modelsService.update(models);
    }

    @ResponseBody
    @PostMapping("api/model/create")
    public JsonResult create(@RequestBody Models models) {
        return modelsService.create(models);
    }

    @ResponseBody
    @PostMapping("/api/model/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file) {
        return Upload.upload(file, "music");
    }

    @ResponseBody
    @DeleteMapping("/api/model/delete")
    public JsonResult delete(@RequestBody Map<String, Integer> map) {
        return modelsService.delete(map.get("id"));
    }
}
